<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
    >
    <!-- <el-form-item>
        <el-input
          v-model="dataForm.key"
          placeholder="请输入角色名称"
          clearable
        ></el-input>
      </el-form-item> -->
      <el-form-item>
        <!-- <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="resetting()">重置</el-button> -->
        <!-- <el-button type="primary">职务管理</el-button> -->
        <el-button type="primary" @click="adds()">新增角色</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="roleName"
        header-align="center"
        align="center"
        label="角色"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间"
      >
      <template slot-scope="scope">
          <span >{{
              getStrTime(scope.row.createTime) 
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="adds(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.roleId)"
            >权限配置</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.roleId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
    <Adds
      v-if="addOrUpdateVisibles"
      ref="addOrUpdates"
      @refreshDataList="getDataList"
    ></Adds>
  </div>
</template>

<script>
import AddOrUpdate from './role-update'
import Adds from './role-add'
export default {
  data () {
    return {
      dataForm: {
        key: '',
        status: null,
        positionId: null
      },
      options: [
        {
          value: null,
          label: '全部'
        },
        {
          value: true,
          label: '1'
        },
        {
          value: false,
          label: '2'
        },
        {
          value: false,
          label: '3'
        }
      ],
      optionTwo: [
        {
          value: null,
          label: '全部'
        },
        {
          value: true,
          label: '启用'
        },
        {
          value: false,
          label: '禁用'
        }
      ],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      addOrUpdateVisibles: false
    }
  },
  components: {
    AddOrUpdate,
    Adds
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      // var positionId = {};
      // var status = {};
      // this.dataListLoading = true;
      // if (this.dataForm.positionId !== null) {
      //   positionId.positionId = this.dataForm.positionId;
      // }
      // if (this.dataForm.status !== null) {
      //   status.status = this.dataForm.status;
      // }
      this.$http({
        url: this.$http.adornUrl(`/admin/role/page`),
        method: 'post',
        data: this.$http.adornData({
          pageNum: this.pageIndex,
          pageSize: this.pageSize
        })
      }).then(({ data }) => {
        if (data && data.code === '200') {
          this.dataList = data.list
          this.totalPage = data.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
     // 新增
    adds (id) {
      this.addOrUpdateVisibles = true
      this.$nextTick(() => {
        this.$refs.addOrUpdates.init(id)
      })
    },
    // 删除
    deleteHandle (id) {
      this.$confirm(
        `确定删除?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl('/admin/role/delete'),
          method: 'post',
          data: this.$http.adornData({
            roleId: id
        })
        }).then(({ data }) => {
          if (data && data.code === '200') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
